﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using SCMS.Model;

namespace SCMS.CoreBusinessLogic.GeneralHelper
{
    public class GeneralHelperService : IGeneralHelperService<Guid, EntityObject>
    {
        //http://blogs.infosupport.com/mvc4autofacentityframework/

        public void Add(EntityObject entityy)
        {
            PerformAction((context) =>
            {
                //context.CreateObjectSet<EntityCollection>.Product.Add(entityy);
                context.AddObject(entityy.ToString(), entityy);
                context.SaveChanges();
            });
        }

        public void Delete(EntityObject entity)
        {
            throw new NotImplementedException();
        }

        public void Update(EntityObject entity)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<EntityObject> GetAll()
        {
            throw new NotImplementedException();
        }

        public EntityObject GetById(Guid id)
        {
            throw new NotImplementedException();
        }

        private void PerformAction(Action<SCMSEntities> toPerform)
        {
            using (var ents = new SCMSEntities())
            {
                ConfigureDbContext(ents);
                toPerform(ents);
            }
        }

        private T Read<T>(Func<SCMSEntities, T> toPerform)
        {
            using (var ents = new SCMSEntities())
            {
                ConfigureDbContext(ents);
                return toPerform(ents);
            }
        }

        private void ConfigureDbContext(SCMSEntities ents)
        {
            //ents.Configuration.AutoDetectChangesEnabled = false;
            //ents.Configuration.LazyLoadingEnabled = false;
            //ents.Configuration.ProxyCreationEnabled = false;
            //ents.Configuration.ValidateOnSaveEnabled = true;

        }
    }
}
